<template>
  <div class="account-box">
    <div class="toggle">
      <a @click="isMsgLogin = false" href="javascript:;" v-if="isMsgLogin">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a @click="isMsgLogin = true" href="javascript:;" v-else>
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>
    <Form
      class="form"
      ref="FormCom"
      :validation-schema="VeeValidateSchema"
      v-slot="{ errors }"
    >
      <template v-if="!isMsgLogin">
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field
              :class="{ error: errors.account }"
              type="text"
              v-model="form.account"
              name="account"
              placeholder="请输入用户名或手机号"
            >
            </Field>
          </div>
          <div class="error" v-if="errors.account">
            <i class="iconfont icon-warning" />{{ errors.account }}
          </div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <Field
              type="password"
              v-model="form.password"
              name="password"
              placeholder="请输入密码"
            ></Field>
          </div>
          <div class="error" v-if="errors.password">
            <i class="iconfont icon-warning" />{{ errors.password }}
          </div>
        </div>
      </template>
      <template v-else>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field
              type="text"
              v-model="form.mobile"
              name="mobile"
              placeholder="请输入手机号"
            ></Field>
          </div>
          <div class="error" v-if="errors.mobile">
            <i class="iconfont icon-warning" />{{ errors.mobile }}
          </div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <Field
              type="password"
              v-model="form.code"
              name="code"
              placeholder="请输入验证码"
            ></Field>
            <div class="error" v-if="errors.code">
              <i class="iconfont icon-warning" />{{ errors.code }}
            </div>
            <span class="code" @click="btnCode">{{
              time === 0 ? "发送验证码" : `${time}后发送`
            }}</span>
          </div>
        </div>
      </template>
      <div class="form-item">
        <div class="agree">
          <!-- <XtxCheckbox v-model="form.isAgree" /> -->
          <Field as="XtxCheckbox" name="isAgree" v-model="form.isAgree" />
          <span>我已同意</span>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <div class="error" v-if="errors.isAgree">
          <i class="iconfont icon-warning" />{{ errors.isAgree }}
        </div>
      </div>
      <a href="javascript:;" class="btn" @click="loginBtn">登录</a>
    </Form>
    <div class="action">
      <a
        href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=100556005&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fwww.corho.com%3A8080%2F%23%2Flogin%2Fcallback"
      >
        <img
          src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png"
          alt=""
        />
      </a>
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>
<script>
import { onUnmounted, reactive, ref, watch } from 'vue'
import { Form, Field } from 'vee-validate'
import Message from '@/utils/Message'
import {
  userAccountLogin,
  userMobileLoginMsg,
  userMobileLogin
} from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import VeeValidateSchema from '@/utils/vee-validate-schema'
import { useIntervalFn } from '@vueuse/core'
export default {
  name: 'LoginForm',
  components: { Form, Field },
  setup () {
    // 是否短信登录
    const isMsgLogin = ref(false)
    const store = useStore()
    const router = useRouter()
    const time = ref(0)

    // 表单信息对象
    const form = reactive({
      account: '',
      password: '',
      mobile: '',
      code: '',
      isAgree: true
    })
    const FormCom = ref(null)
    // 数据的重置
    watch(isMsgLogin, () => {
      // 清除表单信息
      form.account = null
      form.password = null
      form.mobile = null
      form.code = null
      form.isAgree = true
      // 清除错误信息
      FormCom.value.resetForm()
    })
    const { pause, resume } = useIntervalFn(() => {
      time.value--
      if (time.value <= 0) {
        pause()
      }
    }, 1000, {
      immediate: false
    })
    const loginBtn = async () => {
      const res = await FormCom.value.validate()
      try {
        if (res) {
          let results = null
          if (isMsgLogin.value) {
            const { result } = await userMobileLogin({
              mobile: form.mobile,
              code: form.code
            })
            results = result
          } else {
            const { result } = await userAccountLogin({
              account: form.account,
              password: form.password
            })
            results = result
          }
          store.commit('user/setUser', results)
          router.push(router.currentRoute.value.query.url || '/')
          Message({ title: '登录成功', type: 'success' })
        }
      } catch (error) {
        Message({ title: error.response.data.message, type: 'error' })
      }
    }
    const btnCode = async () => {
      const validate = VeeValidateSchema.mobile(form.mobile)
      if (validate === true) {
        try {
          await userMobileLoginMsg(form.mobile)
          Message({ title: '发送成功', type: 'success' })
          time.value = 60
          resume()
        } catch (error) {
          Message({ title: error.response.data.message, type: 'error' })
        }
      } else {
        FormCom.value.setFieldError('mobile', validate)
      }
    }
    onUnmounted(() => {
      pause()
    })
    return {
      btnCode,
      isMsgLogin,
      time,
      form,
      FormCom,
      loginBtn,
      VeeValidateSchema
    }
  }
}
</script>
<style lang="less" scoped>
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 40px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
